<template>
	<div></div>
</template>

<script>
	export default {
		data() {
			return {
				networkTimer: null,
				networkCount: 0
			}
		},
		created() {
			window.addEventListener('online', this.onlineChange);
			window.addEventListener('offline', this.onlineChange);
			this.networkTimer = setInterval(() => {
				this.networkChange()
			}, 1000);
		},
		beforeDestroy() {
			window.removeEventListener('online', this.onlineChange);
			window.removeEventListener('offline', this.onlineChange);
			clearInterval(this.networkTimer)
			this.networkTimer = null
		},
		methods: {
			onlineChange(event) {
				const type = event.type === 'offline' ? 'error' : 'success'
				const title = type === 'error' ? '网络已断开连接!' : '网络已重新连接!'
				this.$message({
					type,
					message: title,
					duration: 5000,
					showClose: true
				})
			},
			networkChange() {
				let rtt = navigator.connection.rtt;
				let {subscribers,publisher,session, OV} = this.$parent
				if (rtt > 1500) {
					this.networkCount += 1
					if (this.networkCount === 3) {
						session.unpublish(publisher);
						for (let i in subscribers) {
							session.unsubscribe(subscribers[i]);
						}
					}
				} else {
					if (this.networkCount > 3) {
						session.publish(publisher);
						for (let i in subscribers) {
							session.subscribe(subscribers[i]);
						}
					}
					this.networkCount = 0
				}
			}
		}
	}
</script>

<style>
</style>